<?php

namespace app\controllers;

use app\lib\AppController;
use app\models\QuestionBankModel;
use app\models\ShieldQuestionBankModel;
use Yii;

class QuestionBankController extends AppController
{
    public function actionList(){
        $data = QuestionBankModel::find()->all();
        return [
            'code'=>0,
            'data'=>$data
        ];
    }
    public function actionGetOne(){
        $ids = QuestionBankModel::find()->select("id")->asArray()->all();
        if(!empty($ids)){
            $id = $ids[array_rand($ids, 1)]['id'];
            $data = QuestionBankModel::find()->where(['id'=>$id])->one();
            return [
                'code'=>0,
                'data'=>$data
            ];
        }else{
            return [
                'code'=>1,
                'message'=>'操作异常'
            ];
        }
    }
    public function actionGetList($num=5){
        $shield = ShieldQuestionBankModel::find()->where(['uid'=>1])->one();//查询该用户屏蔽的题目
        if(!empty($shield)){
            $shieldIds = explode(',',$shield->questionNumber);
            $ids = QuestionBankModel::find()->select("id")->where(['NOT IN','id',$shieldIds])->asArray()->all();//如果
        }else{
            $ids = QuestionBankModel::find()->select("id")->asArray()->all();
        }

        if(!empty($ids)){
            if($num>count($ids)){
                $data = QuestionBankModel::find()->where(['in','id',$ids])->asArray()->all();
                shuffle($data);
                return [
                    'code'=>0,
                    'data'=>$data
                ];
            }
            $idRand = array_rand($ids, $num);
            $idOpt = [];
            foreach ($idRand as $k=>$v){
                $idOpt[] = $ids[$v]['id'];
            }
            $data = QuestionBankModel::find()->where(['in','id',$idOpt])->all();
            shuffle($data);
            return [
                'code'=>0,
                'data'=>$data
            ];
        }else{
            return [
                'code'=>1,
                'message'=>'操作异常'
            ];
        }
    }



}
